package com.autohammal.task;

import java.io.Serializable;
import java.util.Hashtable;

import com.autohammal.ITarget;
import com.autohammal.ITask;
import com.autohammal.TaskAdapter;
import com.autohammal.TaskListener;

/**
 * abstract task as a thread
 * 
 * @author dailey_dai Feb 18, 2011
 */
public abstract class Task implements ITask, Serializable {

	private static final long serialVersionUID = -8250237156898512046L;
	private String taskName = TASK_NAME_DEFAULT;
	private Hashtable<String, Object> inputItems = null;
	private Hashtable<String, Object> outputItems = null;
	private volatile TaskStatus status = TaskStatus.UNCOMPLETE;
	private TaskListener taskListener = null;
	public static final String TASK_NAME_DEFAULT = "task";
	private ITarget target = null;

	public Task() {
		initialTaskListener();
		setTaskName(TASK_NAME_DEFAULT);
	}

	public Task(ITarget target) {
		initialTaskListener();
		setTarget(target);
		setTaskName(TASK_NAME_DEFAULT);
	}

	public void setTarget(final ITarget target) {
		this.target = target;
	}

	public ITarget getTarget() {
		return target;
	}

	private void initialTaskListener() {
		taskListener = null;
		taskListener = new TaskAdapter();
	}

	@Override
	public void addTaskListener(TaskListener taskListener) {
		if (taskListener == null)
			initialTaskListener();
		else
			this.taskListener = taskListener;
	}

	@Override
	public TaskStatus getTaskStatus() {
		return status;
	}

	@Override
	public void setTaskStatus(TaskStatus status) {
		this.status = status;
	}

	@Override
	public TaskListener getTaskListener() {
		if (taskListener == null)
			initialTaskListener();
		return taskListener;
	}

	@Override
	public void removeTaskListener() {
		this.initialTaskListener();
	}

	@Override
	public abstract void executeTask();

	@Override
	public Object getInputItem(String key) {
		if (inputItems != null)
			return inputItems.get(key);
		return null;
	}

	@Override
	public Object getOutputItem(String key) {
		if (outputItems != null)
			return outputItems.get(key);
		return null;
	}

	@Override
	public String getTaskDescription() {
		return null;
	}

	@Override
	public String getTaskId() {
		return "TN_" + getTaskName();
	}

	@Override
	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		if (taskName != null)
			this.taskName = taskName;
	}

	@Override
	public void setInputItem(String key, Object value) {
		if (inputItems == null)
			inputItems = new Hashtable<String, Object>();
		inputItems.put(key, value);
	}

	@Override
	public void setOutputItem(String key, Object value) {
		if (outputItems == null)
			outputItems = new Hashtable<String, Object>();
		outputItems.put(key, value);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;

		if (obj == this)
			return true;

		if (obj instanceof ITask) {
			ITask task = (ITask) obj;
			return this.getTaskId().equals(task.getTaskId());
		}

		return false;
	}
}
